﻿namespace DotNetUtility.ExtensionMethods
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// To check whether the given object is equal to null.
        /// </summary>
        /// <param name="object">The object to check</param>
        /// <returns>Whether it is null or not.</returns>
        /// <example>
        ///     <code>
        ///         object objectToCheck = null;
        ///         var result = objectToCheck.IsNotEqualToNull();        
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool IsEqualToNull(this object @object)
        {
            return ReferenceEquals(@object, null);
        }

        /// <summary>
        /// To check whether the given object is not equal to null.
        /// </summary>
        /// <param name="object">The object to check</param>
        /// <returns>Whether it is not null or not.</returns>
        /// <example>
        ///     <code>
        ///         object objectToCheck = null;
        ///         var result = objectToCheck.IsNotEqualToNull();
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool IsNotEqualToNull(this object @object)
        {
            return !@object.IsEqualToNull();
        }

        /// <summary>
        /// To check whether two objects are same type.
        /// </summary>
        /// <param name="object1"></param>
        /// <param name="object2"></param>
        /// <returns>true oor false</returns>
        /// <example>
        ///     <code>
        ///         PersonA personA = new PersonA();
        ///         PersonB personB = new PersonB();
        ///         bool result1 = personA.IsSameTypeAs(personB);
        ///         bool result2 = personA.IsSameTypeAs("personB");
        ///         bool result3 = personA.IsSameTypeAs(null);
        ///         bool result4 = default(object).IsSameTypeAs(personB);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool IsSameTypeAs(this object @object1, object @object2)
        {
            return @object1.IsNotEqualToNull() && @object2.IsNotEqualToNull() && @object1.GetType() == @object2.GetType();
        }

        /// <summary>
        /// To check whether two objects implement same interface type of T. 
        /// </summary>
        /// <typeparam name="T">The type of interface to check</typeparam>
        /// <param name="object1"></param>
        /// <param name="object2"></param>
        /// <returns>true or false</returns>
        /// <example>
        ///     <code>
        ///         PersonA personA = new PersonA();
        ///         PersonB personB = new PersonB();
        ///         bool result5 = personA.IsImplementSameInterface<IPerson>(personB);
        ///         bool result6 = personA.IsImplementSameInterface<IPerson>(null);
        ///         bool result7 = default(object).IsImplementSameInterface<IPerson>(personB);
        ///         bool result8 = personA.IsImplementSameInterface<PersonA>(personA);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool IsImplementSameInterface<T>(this object @object1, object @object2)
        {
            return
                @object1.IsNotEqualToNull() && @object2.IsNotEqualToNull() && typeof(T).IsInterface &&
                @object1.GetType().GetInterface(typeof(T).Name).IsNotEqualToNull() &&
                @object2.GetType().GetInterface(typeof(T).Name).IsNotEqualToNull();
        }
    }
}
